//
//  LSHomeTableViewCell.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/7.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit
import SnapKit
class LSHomeTableViewCell: UITableViewCell {
    
    //保存一个约束 存在约束的重用  也就是说cell出来之后约束就释放了, 然后再添加,再释放
    var bottomConstraint: Constraint?
    
    var statusViewModel :LSStatusViewModel?{
        didSet{
            // 在赋值的时候回调用这个方法，did 在英文里是 已经的意思
            orginalView.orginalViewModel = statusViewModel
            
            footerView.footerViewModel =  statusViewModel
            
            // 存在一个约束重用
            bottomConstraint?.uninstall()
            
            //判断有没有转发微博
            if statusViewModel?.status?.retweeted_status == nil {
                //原创微博
                
                retweetView.hidden = true
                /**
                *  constraint  是一个关键词  要记住  是约束的一个关键词!
                */
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.top.equalTo(orginalView.snp_bottom).constraint
                })
        
            } else {
                
                //转发微博
                retweetView.hidden = false
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                //给转发微博赋值
                retweetView.retweetViewModel = statusViewModel
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        
        //把原创微博view添加进cell
        contentView.addSubview(orginalView)
        //添加底部功能VIEW
        contentView.addSubview(footerView)
        //添加转发微博
        contentView.addSubview(retweetView)
        
        //设置约束
        orginalView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(orginalView.snp_bottom)
        }
        
        
        footerView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            
            bottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.height.equalTo(44)
        }
        
        //自动计算行高1.需要设置contentView的边界
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self).offset(10)
            
            make.bottom.equalTo(footerView.snp_bottom)
            
            
        }
        
    }
    

    
    //MARK:--懒加载控件
    private lazy var orginalView :LSOrginalView = LSOrginalView()
    
    private lazy var footerView:LSFooterView = LSFooterView()
    
    private lazy var retweetView:LSRetweetView = LSRetweetView()
}
